﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Expression.Encoder.Devices;
using WebcamControl;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;


using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Math;
using AForge.Math.Geometry;
using System.Runtime.InteropServices;

namespace WPF_Webcam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();                   
                        
            Binding binding_1 = new Binding("SelectedValue");
            binding_1.Source = VideoDevicesComboBox;
            WebcamCtrl.SetBinding(Webcam.VideoDeviceProperty, binding_1);

            // Create directory for saving video files
            string videoPath = @"C:\VideoClips";

            if (! Directory.Exists(videoPath))
            {
                Directory.CreateDirectory(videoPath);
            }
            // Create directory for saving image files
            string imagePath = @"C:\WebcamSnapshots";

            if (! Directory.Exists(imagePath))
            {
                Directory.CreateDirectory(imagePath);
            }

            // Set some properties of the Webcam control
            WebcamCtrl.VideoDirectory = videoPath;
            WebcamCtrl.ImageDirectory = imagePath;
            WebcamCtrl.FrameRate = 30;
            WebcamCtrl.FrameSize = new System.Drawing.Size(640, 480);

            // Find available a/v devices
            var vidDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video);
           
            VideoDevicesComboBox.ItemsSource = vidDevices;
           
            VideoDevicesComboBox.SelectedIndex = 0;
           
        }        

        private void StartCaptureButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Display webcam video
                PictureBox1.Visibility = Visibility.Hidden;
                WebcamCtrl.Visibility = Visibility.Visible;
                WebcamCtrl.StartCapture();               
            }
            catch (Microsoft.Expression.Encoder.SystemErrorException)
            {
                MessageBox.Show("Device is in use by another application");
            }
        }

        private void StopCaptureButton_Click(object sender, RoutedEventArgs e)
        {
            // Stop the display of webcam video.
            WebcamCtrl.StopCapture();
        }

        private void TakeSnapshotButton_Click(object sender, RoutedEventArgs e)
        {
            // Take snapshot of webcam video.
            
            WebcamCtrl.TakeSnapshot();
            DateTime _vrijemeOcitavanja = DateTime.Now;
            int sati = 0;
            string _pmam = "";
            if (_vrijemeOcitavanja.Hour < 24  && _vrijemeOcitavanja.Hour > 12)
            {
                sati = _vrijemeOcitavanja.Hour - 12;
                _pmam = " PM";
            }
            else 
            {
                sati = _vrijemeOcitavanja.Hour;
                _pmam = " AM";
            }

            string sekunde = "0";
            if (_vrijemeOcitavanja.Second < 10)
            {
                sekunde = sekunde + _vrijemeOcitavanja.Second.ToString();
            }
            else sekunde
                = _vrijemeOcitavanja.Second.ToString();


            string minute = "0";
            if (_vrijemeOcitavanja.Minute < 10)
            {
                minute = minute + _vrijemeOcitavanja.Minute.ToString();
            }
            else minute
                = _vrijemeOcitavanja.Minute.ToString();
            string path ="Snapshot "+ _vrijemeOcitavanja.Month.ToString() + "-" + _vrijemeOcitavanja.Day.ToString() + "-" + _vrijemeOcitavanja.Year.ToString() + " " + sati.ToString() + "."+ minute + "." + sekunde.ToString();
            
                
            
            ImageSource images = new BitmapImage(new Uri(@"C:\WebcamSnapshots\"+path+_pmam+".Jpeg"));
            PictureBox1.Source = images;
            
            

            WebcamCtrl.StopCapture();
            WebcamCtrl.Visibility = Visibility.Hidden;
            PictureBox1.Visibility = Visibility.Visible;
            System.Windows.Forms.MessageBox.Show("Barkod je ispravan i uspjesno spremljen.");


            /*
            ImageSourceConverter c = new ImageSourceConverter();
            Bitmap imageX;
            imageX = (Bitmap)c.ConvertFrom(images);
            //imageX = images as Bitmap;
            System.Drawing.Bitmap image2 = AForge.Imaging.Image.Clone(imageX, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            tessnet2.Tesseract ocr = new tessnet2.Tesseract();
            ocr.Init(@"C:\Users\Aida\Documents\Visual Studio 2012\Projects\studentskicentarETFa(13)\studentskicentarETFa\WpfApplication1\bin\Debug\tessdata", "eng", false);
            

            List<tessnet2.Word> result = ocr.DoOCR(imageX, System.Drawing.Rectangle.Empty);

            string t = "";
            foreach (tessnet2.Word word in result)
            {
                t = " " + word.Text + Environment.NewLine;
            }
            MessageBox.Show(t);
            
            */
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

            WpfApplication1.MainWindow m = new WpfApplication1.MainWindow();
            m.Show();
            this.Hide();
        } 
    }
}
